import produce from 'immer'
import { Dispatch } from 'redux'
import { E_A_MESSAGE_TYPE } from '../action-types'
import { IAction } from '../store-types'
import { createAction } from '../store-util'

export interface IMessageState {
  newMsgCount: number
}

const initialState: IMessageState = {
  newMsgCount: 0
}

export interface IAuthAction {
  setNewMsgCount(newMsgCount: number): void
}

export const setNewMsgCount = (newMsgCount: number) => (dispatch: Dispatch) => {
  dispatch(createAction(E_A_MESSAGE_TYPE.SET_MESSAGE_COUNT, newMsgCount))
}

export const getNewMsgCount = () => async (dispatch: Dispatch) => {
  // todo
}

export default function reducer(state: IMessageState = initialState, action: IAction) {
  switch (action.type) {
    case E_A_MESSAGE_TYPE.SET_MESSAGE_COUNT:
      return produce(state, draft => {
        draft.newMsgCount = action.payload as number
      })
    default:
      return state
  }
}

declare module '../store-types' {
  interface IGlobalState {
    message: IMessageState
  }
}
